{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "91939d99",
   "metadata": {},
   "source": [
    "# Example: Evaluate Ensemble Performance\n",
    "\n",
    "In this notebook, we shall demonstrate how to evaluate the performance of ensembles. For this, evaluations of models must be available locally **along with their stored forecasts**, e.g. by using the CLI to download the publicly available evaluations:\n",
    "\n",
    "```bash\n",
    "tsbench evaluations download --include_forecasts\n",
    "```\n",
    "\n",
    "_**Note:** The publicly available forecasts require roughly 600 GiB of available local storage._"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ae3df8d",
   "metadata": {},
   "source": [
    "## Initialize the Tracker\n",
    "\n",
    "As you have seen in the `browse-offline-evaluations` notebook, we use the tracker for accessing the evaluations that are available offline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "999cacc1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from tsbench.evaluations.tracking import ModelTracker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "617defa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "tracker = ModelTracker.from_directory(Path.home() / \"evaluations\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "129bcf7c",
   "metadata": {},
   "source": [
    "## Manual Evaluation\n",
    "\n",
    "For this example, we want to analyze the performance of a simple combination of two deep learning models (TFT and DeepAR) and a classical model (ARIMA) and compare it to the performance of the individual models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0322a686",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tsbench.config import DATASET_REGISTRY, Config\n",
    "from tsbench.config.model.models import (\n",
    "    TemporalFusionTransformerModelConfig,\n",
    "    DeepARModelConfig,\n",
    "    ARIMAModelConfig,\n",
    ")\n",
    "from tsbench.analysis import EnsembleAnalyzer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6d3414e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "analyzer = EnsembleAnalyzer(tracker)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e88ede54",
   "metadata": {},
   "outputs": [],
   "source": [
    "members = [\n",
    "    TemporalFusionTransformerModelConfig(),\n",
    "    DeepARModelConfig(),\n",
    "    ARIMAModelConfig(),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fe79e023",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = DATASET_REGISTRY[\"kdd_2018\"]()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "74be4018",
   "metadata": {},
   "outputs": [],
   "source": [
    "ensemble_performance = analyzer.get_ensemble_performance(members, dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "caca5bca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Metric(mean=0.4105578065745702, std=0.0247243410990787)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ensemble_performance.ncrps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "28fc1968",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tft : Metric(mean=0.4358486086130142, std=0.011365100741386414)\n",
      "deepar : Metric(mean=0.41372910141944885, std=0.08784350752830505)\n",
      "arima : Metric(mean=0.5097207129001617, std=0.0018174946308135986)\n"
     ]
    }
   ],
   "source": [
    "for member in members:\n",
    "    print(\n",
    "        member.name(),\n",
    "        \":\",\n",
    "        tracker.get_performance(Config(member, dataset)).ncrps,\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63aeb442",
   "metadata": {},
   "source": [
    "As we can see in this simple example, the performance of the ensemble in terms of nCRPS improves upon the performance of the individual models."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a8643ee",
   "metadata": {},
   "source": [
    "## Exhaustive Evaluation\n",
    "\n",
    "In case you want to analyze the performance of ensembles across datasets, you should use the `tsbench` CLI. Check out ensemble analysis command:\n",
    "\n",
    "```bash\n",
    "tsbench analysis ensemble --help\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51ac46bc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
